package control;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import control.ConnectionMysql;
import control.Usuario;
 
public class DaoUsuario {
	public void addUsuario(Usuario usuario) {
    	try {
    		String query = "insert into usuario" +
                    " (email,senha,nome,apelido,dataNasc)" +
                    " values (?,?,?,?,?)";
   
    		ConnectionMysql conecta = new ConnectionMysql();
    		Connection conexao 	= 	conecta.newConnection();			
    		PreparedStatement ps = 	conexao.prepareStatement(query);
    		
        
    		ps.setString(1, usuario.getEmail());
    		ps.setString(2, usuario.getSenha());
    		ps.setString(3, usuario.getNome());
    		ps.setString(4, usuario.getApelido());
    		ps.setString(5, usuario.getDataNasc());
    		ps.execute();
            ps.close();
            conexao.close();
        }
    	catch( Exception e){
            System.out.println("erro: "+e.getMessage());
    	      
    	}
    }
    public boolean existeUsuario(String email) throws Exception {
    	
    		String query="Select * from usuario where email = '"+email+"'";    		
    		ConnectionMysql conecta = new ConnectionMysql();
    		Connection conexao 				= 	conecta.newConnection();			
    		Statement statement 			= 	conexao.createStatement();
    	    ResultSet rs 					= 	null;
    		
    	    rs = statement.executeQuery( query );
			if (rs.next()) {
				conexao.close();
				rs.close();
				return true;
			} else {
				conexao.close();
				rs.close();
				return false;
			}
    	   
    	
		
	}
    public boolean verificaSenha(String email, String senha) throws Exception {
    	String query="Select * from usuario where email = '"+email+"'";    		
		ConnectionMysql conecta = new ConnectionMysql();
		Connection conexao 				= 	conecta.newConnection();			
		Statement statement 			= 	conexao.createStatement();
	    ResultSet rs 					= 	null;
		
	    rs = statement.executeQuery( query );
	    rs.first();
		if (rs.getString(1).equals(senha)) {
			conexao.close();
			rs.close();
			return true;
		} else {
			conexao.close();
			rs.close();
			return false;
		}
    	
    	
    }
}    